using System;
using System.Drawing;
using System.Windows.Forms;
using ProgramLauncher.Models;

namespace ProgramLauncher
{
    public class ButtonConfigForm : Form
    {
        private readonly TextBox nameTextBox;
        private readonly TextBox pathTextBox;
        private readonly Button browseButton;
        private readonly Button okButton;
        private readonly Button cancelButton;

        public ButtonConfig ButtonConfig { get; private set; }

        public ButtonConfigForm(ButtonConfig config)
        {
            // 名称标签
            var nameLabel = new Label
            {
                Text = "按钮名称：",
                AutoSize = true,
                Location = new Point(30, 30),
                Font = new Font("Microsoft YaHei UI", 12F)
            };
            Controls.Add(nameLabel);

            // 名称文本框
            nameTextBox = new TextBox
            {
                Location = new Point(30, 70),
                Size = new Size(440, 35),
                Font = new Font("Microsoft YaHei UI", 12F),
                Text = config?.Name ?? ""
            };
            nameTextBox.TextChanged += ValidateInput;
            Controls.Add(nameTextBox);

            // 路径标签
            var pathLabel = new Label
            {
                Text = "程序路径：",
                AutoSize = true,
                Location = new Point(30, 130),
                Font = new Font("Microsoft YaHei UI", 12F)
            };
            Controls.Add(pathLabel);

            // 路径文本框
            pathTextBox = new TextBox
            {
                Location = new Point(30, 170),
                Size = new Size(340, 35),
                Font = new Font("Microsoft YaHei UI", 12F),
                Text = config?.ProgramPath ?? ""
            };
            pathTextBox.TextChanged += ValidateInput;
            Controls.Add(pathTextBox);

            // 浏览按钮
            browseButton = new Button
            {
                Text = "浏览...",
                Location = new Point(380, 170),
                Size = new Size(90, 35),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(240, 240, 240),
                FlatStyle = FlatStyle.Flat,
                Cursor = Cursors.Hand
            };
            browseButton.FlatAppearance.BorderSize = 1;
            browseButton.Click += BrowseButton_Click;
            Controls.Add(browseButton);

            // 确定按钮
            okButton = new Button
            {
                Text = "确定",
                Location = new Point(240, 240),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(0, 120, 215),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.OK,
                Enabled = false,
                Cursor = Cursors.Hand
            };
            okButton.FlatAppearance.BorderSize = 0;
            okButton.Click += OkButton_Click;
            Controls.Add(okButton);

            // 取消按钮
            cancelButton = new Button
            {
                Text = "取消",
                Location = new Point(370, 240),
                Size = new Size(120, 50),
                Font = new Font("Microsoft YaHei UI", 12F),
                BackColor = Color.FromArgb(240, 240, 240),
                FlatStyle = FlatStyle.Flat,
                DialogResult = DialogResult.Cancel,
                Cursor = Cursors.Hand
            };
            cancelButton.FlatAppearance.BorderSize = 1;
            Controls.Add(cancelButton);

            // 窗体设置
            Text = config == null ? "添加按钮" : "编辑按钮";
            ClientSize = new Size(500, 320);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            StartPosition = FormStartPosition.CenterParent;
            MaximizeBox = false;
            MinimizeBox = false;
            BackColor = Color.White;

            // 验证输入
            ValidateInput(null, EventArgs.Empty);
        }

        private void ValidateInput(object sender, EventArgs e)
        {
            okButton.Enabled = !string.IsNullOrWhiteSpace(nameTextBox.Text) &&
                             !string.IsNullOrWhiteSpace(pathTextBox.Text);
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            using (var dialog = new OpenFileDialog())
            {
                dialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*";
                dialog.FilterIndex = 1;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    pathTextBox.Text = dialog.FileName;
                }
            }
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            ButtonConfig = new ButtonConfig
            {
                Name = nameTextBox.Text.Trim(),
                ProgramPath = pathTextBox.Text.Trim()
            };
        }
    }
} 